package com.sxt;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class GameWin extends JFrame {
    //state：控制游戏的不同状态
    /**游戏状态 0未开始，1游戏中，2通关失败，3通过成功，4暂停*/
    static int state = 0;

    //双缓冲图像对象,避免画面闪烁
    Image offScreenImage;

    //宽高
    int width = 1440;
    int height = 900;

    double random;
    //计时器
    int time = 0;

    //背景对象
    Bg bg = new Bg();

    //敌方鱼类
    Enamy enamy ;
    //boss类
    Enamy boss;
    //是否生成boss
    boolean isBoss = false;

    //我方鱼类
    MyFish myFish = new MyFish();


    // 游戏线程
    private Thread gameThread;


    public void launch(){
        this.setVisible(true);
        this.setSize(width,height);
        // 窗口居中
        this.setLocationRelativeTo(null);
        //this.setResizable(false);
        this.setTitle("大鱼吃小鱼");
        // 关闭窗口时退出程序
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // 添加鼠标监听器
        this.addMouseListener(new MouseAdapter()  {
            @Override
            public void mouseClicked(MouseEvent e) {

                super.mouseClicked(e);
                // 左键点击开始游戏
                if (e.getButton() == 1 && state == 0) {
                    state = 1;

                    repaint();
                }

                // 左键点击重新开始
                if (e.getButton() == 1 && (state == 2 || state == 3)) {
                    reGame();
                    state = 1;

                }
            }
        });

        //添加键盘监听器,键盘移动
        this.addKeyListener(new KeyAdapter(){
            @Override//按压
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                //按WASD
                if (e.getKeyCode()==87){
                    GameUtils.UP = true;
                }
                if (e.getKeyCode()==83){
                    GameUtils.DOWN = true;
                }
                if (e.getKeyCode()==65){
                    GameUtils.LEFT = true;
                }
                if (e.getKeyCode()==68){
                    GameUtils.RIGHT = true;
                }
                //按下空格键暂停/恢复
                if (e.getKeyCode()==32){
                    switch (state){
                        case 1:
                            state = 4;
                            GameUtils.drawWord(getGraphics(),"游戏暂停！！！",Color.red,50,600,400);
                            break;
                        case 4:
                            state = 1;
                            break;
                    }
                }
            }

            @Override//松开WASD停止移动
            public void keyReleased(KeyEvent e) {
                super.keyReleased(e);
                if (e.getKeyCode()==87){
                    GameUtils.UP = false;
                }
                if (e.getKeyCode()==83){
                    GameUtils.DOWN = false;
                }
                if (e.getKeyCode()==65){
                    GameUtils.LEFT = false;
                }
                if (e.getKeyCode()==68){
                    GameUtils.RIGHT = false;
                }
            }

        });



       //主循环：持续更新游戏画面和逻辑
        while (true){
            repaint();//刷新界面
            time++;

            try {
               Thread.sleep(50); //每40ms刷新一次
           } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

   }



    public void paint(Graphics g){
        // 创建双缓冲图像
        offScreenImage = createImage(width,height);
        Graphics gImage = offScreenImage.getGraphics();

        // 绘制背景
        bg.paintSelf(gImage ,myFish.level);

        switch (state){
            case 0:// 未开始状态
                break;
            case 1:// 游戏中状态
                myFish.paintSelf(gImage);// 绘制玩家鱼
                logic();// 更新游戏逻辑
                // 绘制敌方鱼
                for(Enamy enamy:GameUtils.EnamyList){
                    enamy.paintSelf(gImage);
                }
                if (isBoss){
                    // 移动Boss
                    boss.x = boss.x+boss.speed*boss.dir;
                    boss.paintSelf(gImage);
                    if (boss.x<0){
                        gImage.setColor(Color.red);
                        // 绘制红色警戒区域
                        gImage.fillRect(boss.x,boss.y,2400,boss.height/30);
                    }
                }
                break;
            case 2: // 通关失败状态
                for(Enamy enamy:GameUtils.EnamyList){
                    enamy.paintSelf(gImage);
                }
                if (isBoss){
                    boss.paintSelf(gImage);
                }
                break;
            case 3: // 通过成功状态
                myFish.paintSelf(gImage);
                break;
            case 4:// 暂停状态
                return;
            default:

        }

        g.drawImage(offScreenImage,0,0,null);// 将双缓冲图像绘制到屏幕上
    }


    void logic(){
        //根据得分调整关卡难度
        if (GameUtils.count<5){
            GameUtils.level = 0;
            myFish.level = 1;
        }else if (GameUtils.count<=15){
            GameUtils.level = 1;
        }else if (GameUtils.count<=50){
            GameUtils.level = 2;
            myFish.level = 2;
        }else if (GameUtils.count<=150){
            GameUtils.level = 3;
            myFish.level = 3;
        }else if (GameUtils.count<=300){
            GameUtils.level = 4;
            myFish.level = 3;
        }else if (GameUtils.count>300){
            state = 3;  // 通过成功
        }


        random = Math.random();

        //根据关卡生成敌方鱼
        switch (GameUtils.level) {
            case 4:
                if (time % 60 == 0) {
                    if (random > 0) {
                        boss = new Enamy_Boss(); // 生成Boss
                        isBoss = true;
                    }
                }
            case 3:
            case 2:
                if (time % 30 == 0){
                    if (random > 0.5) {
                        enamy = new Enamy_3_L(); // 生成高级敌方鱼
                    } else {
                        enamy = new Enamy_3_R();
                    }
                    GameUtils.EnamyList.add(enamy);
                }
            case 1:
                if (time % 20 == 0) {
                    if (random > 0.5) {
                        enamy = new Enamy_2_L(); // 生成中级敌方鱼
                    } else {
                        enamy = new Enamy_2_R();
                    }
                    GameUtils.EnamyList.add(enamy);

                }
            case 0:
            if (time % 10 == 0) {
                if (random > 0.5) {
                    enamy = new Enamy_1_L(); // 生成初级敌方鱼
                } else {
                    enamy = new Enamy_1_R();
                }
                GameUtils.EnamyList.add(enamy);
            }
            break;
            default:
        }
        //移动方向，检测碰撞
        for (Enamy enamy:GameUtils.EnamyList){
            enamy.x = enamy.x+enamy.speed*enamy.dir; // 移动敌方鱼
            if (isBoss){
                if (boss.getRec().intersects(enamy.getRec())){ // Boss吞噬敌方鱼
                    enamy.x = -200;
                    enamy.y = -200;
                }
                if (boss.getRec().intersects(myFish.getRec())){ // 玩家鱼被Boss吞噬
                    state = 2;
                }
            }

            //我方鱼与敌方鱼碰撞检测
            if (myFish.getRec().intersects(enamy.getRec())){ // 玩家鱼与敌方鱼碰撞
                if (myFish.level>=enamy.type) { // 玩家鱼等级大于等于敌方鱼
                    System.out.println("碰撞了");
                    enamy.x = -200;
                    enamy.y = -200;
                    GameUtils.count = GameUtils.count + enamy.count;// 增加得分
                }else {
                    state = 2; // 玩家鱼被吞噬
                }


            }
        }
    }


    //重新开始游戏
    void reGame(){
        // 清空敌方鱼列表，并重置所有游戏相关变量
        GameUtils.EnamyList.clear();
        time = 0;
        myFish.level = 1;
        GameUtils.count = 0;
        myFish.x = 700;
        myFish.y = 500;
        myFish.width = 50;
        myFish.height = 50;
        boss = null;
        isBoss = false;
    }

    public static void main(String[] args) {
        GameWin gameWin = new GameWin(); // 创建游戏窗口对象
        gameWin.launch(); // 启动游戏
    }
}
